package com.example.studentcourse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentcourse.common.Result;
import com.example.studentcourse.entity.Course;
import com.example.studentcourse.service.ICourseService;
import com.example.studentcourse.vo.CourseQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程管理接口
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;

    /**
     * 分页条件查询课程列表
     * @param query 查询参数
     * @return 分页结果
     */
    @PostMapping("/list")
    public Result<Page<Course>> list(@RequestBody CourseQueryVO query) {
        // 构建查询条件
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        // 课程名称模糊查询
        wrapper.like(StringUtils.hasText(query.getName()), Course::getName, query.getName())
                // 教师姓名模糊查询
                .like(StringUtils.hasText(query.getTeacherName()), Course::getTeacherName, query.getTeacherName())
                // 学分精确查询
                .eq(query.getCredit() != null, Course::getCredit, query.getCredit());

        // 执行分页查询
        Page<Course> page = courseService.page(new Page<>(query.getCurrent(), query.getSize()), wrapper);
        return Result.success(page);
    }

    /**
     * 新增课程
     */
    @PostMapping
    public Result<?> save(@RequestBody Course course) {
        courseService.save(course);
        return Result.success(null);
    }

    /**
     * 修改课程信息
     */
    @PutMapping
    public Result<?> update(@RequestBody Course course) {
        courseService.updateById(course);
        return Result.success(null);
    }

    /**
     * 删除单个课程
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        courseService.removeById(id);
        return Result.success(null);
    }

    /**
     * 批量删除课程
     */
    @DeleteMapping("/batch")
    public Result<?> deleteBatch(@RequestBody List<Long> ids) {
        courseService.removeByIds(ids);
        return Result.success(null);
    }
} 